const app = getApp();
const INIT_CALLOUT = {
  content: '',
  padding: 12,
  display: 'ALWAYS',
  fontSize: 14,
  textAlign: 'center',
  borderRadius: 6,
  borderWidth: 2,
  bgColor: '#07c160',
  color: "#fff"
};
const INIT_CALLOUT_MARKER = {
  callout: {
    ...INIT_CALLOUT
  },
  latitude: "",
  longitude: "",
};
Page({
  data: {
    show: false, //地图是否显示
    villages: '', //园区列表
    villageIndex: "", //选中的园区索引
    empty: false,
    name: '',
    none: 1, //显示暂不选择
    showAddBtn: true, //维修员没有添加园区的操作
    latitude: "",
    longitude: "",
    calloutMarkers: [{
      ...INIT_CALLOUT_MARKER
    }],
  },

  onLoad: function (option) {
    this.setData({
      none: option.none || 0
    })
    this.isFranchiser = option.is_franchiser || false;
    this.isRepair = option.is_repair || false;
    let hidden = option.hidden; //hidden为true时会隐藏添加按钮
    if (this.isRepair || hidden) {
      this.setData({
        showAddBtn: false
      })
    }
    let _self = this;
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      if (_self.isFranchiser || _self.isRepair) {
        _self.partner_id = data.partner_id;
      }
      _self.getVillageList(data.village_id, option.mode ? 1 : 0);
    })
  },

  showMap(e) {
    const index = e.currentTarget.dataset.index;
    const village = this.data.villages[index];
    let longitude = village.lng || '';
    let latitude = village.lat || '';
    if (!longitude || !latitude || longitude == 0 || latitude == 0) {
      app.showToast("抱歉！定位失败");
      return;
    }
    console.log(village.village_name, "village.village_name")
    this.setData({
      longitude,
      latitude,
      [`calloutMarkers[0].longitude`]: longitude,
      [`calloutMarkers[0].latitude`]: latitude,
      [`calloutMarkers[0].callout.content`]: village.village_name,
      show: true
    })
  },

  closeMap() {
    this.setData({
      show: false
    })
  },

  //获取园区列表  
  getVillageList(id, mode = 0) {
    let url = "Partner/WaterCard/getPartnerVillage";
    let data = {};
    if (this.isFranchiser) {
      url = "Partner/WaterCard/getPartnerVillage";
      data = {
        row: 9999,
        page: 1,
        isfranchiser: 1,
        partner_id: this.partner_id
      }
      if(mode=1){
        url="Partner/Franchiser/getFranchiserVillage";
        data={};
      }
    }
    if (this.isRepair) {
      url = "Manage/Equipment/repairPersonnelPark";
      data = {
        partner_id: this.partner_id
      }
    }
    app.ajax({
      url,
      data,
      success: res => {
        if (res.data.code > 0) {

          let source = this.isRepair ? res.data.data[0].villageData : res.data.data;
          if (source.length == 0) {
            this.setData({
              empty: true
            })
          } else {
            if (this.data.none == 1) {
              source.unshift({
                id: 0,
                village_name: "",
                province: "",
                city: "",
                area: "",
                address: ""
              });
            }
            let villageIndex = '';
            if (id != undefined) {
              villageIndex = source.findIndex(item => item.id == id);
              if (~villageIndex) { //将选中数据排第一
                source.unshift(source.splice(villageIndex, 1)[0]);
                villageIndex = 0;
              } else {
                villageIndex = "";
              }
            }
            this.setData({
              villages: source,
              empty: false,
              villageIndex,
            })
          }
        } else {
          app.showToast(res.data.message)
        }
      },
      error: e => {
        app.showToast("网络错误")
      }
    })
  },


  //选择园区
  chooseVillage(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      villageIndex: index
    })
  },

  confirm() {
    let index = this.data.villageIndex;
    let village = this.data.villages[index];
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('acceptDataFromMiddleVillage', {
      ...village
    });
    setTimeout(() => {
      wx.navigateBack({
        delta: 1
      })
    }, 500)
  },


  //搜索园区
  filterVillage(e) {
    const val = e.detail.value;
    const list = this.data.villages;
    if (list.length <= 0) {
      return false;
    }
    list.filter(current => {
      if (current.address.includes(val) || current.village_name.includes(val)) {
        current.show = false;
      } else {
        current.show = true;
      }
    })
    this.setData({
      name: val,
      villages: list
    })
  },

  //添加园区
  navAddVillage() {
    let url = "../../editCommunity/editCommunity?type=2&from=middle";
    if (this.isFranchiser) {
      url = "../../franchiseees/editfranchiseeeCommunity/editfranchiseeeCommunity?from=middle&partner_id=" + this.partner_id
    }
    wx.navigateTo({
      url,
    })
  },
})